package com.zk.fm.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zk.fm.R;
import com.zk.fm.util.AppInfo;
import com.zk.fm.util.WebServiceUtil;

public class SuggestActivity extends Activity {
	private Button commit;
	private EditText suggestion;
	private long id;
	private ProgressDialog pd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.suggest);
		initView();
		setListener();
	}

	private void initView() {
		suggestion = (EditText) findViewById(R.id.suggestion);
		commit = (Button) findViewById(R.id.commit);
		pd = new ProgressDialog(this);
		pd.setMessage(getString(R.string.publishing));
		pd.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					id = -1;
				}
				return false;
			}
		});
	}

	private void setListener() {
		commit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (AppInfo.currentAccount == null
						|| AppInfo.currentAccount.equals("")) {
					Toast.makeText(SuggestActivity.this,
							R.string.loginBeforeSuggest, 5).show();
					return;
				}
				boolean validateResult = clientValidate();
				if (!validateResult) {
					return;
				}
				pd.show();
				Thread loginThread = new Thread() {
					public void run() {
						id = this.getId();
						List<Object> l = new ArrayList<Object>();
						l.add(AppInfo.currentAccount);
						l.add(suggestion.getText().toString().trim());
						boolean suggestResult;
						try {
							suggestResult = Boolean.parseBoolean(WebServiceUtil
									.callWebService(
											WebServiceUtil.METHOD_SUGGEST, l,
											10000));
							System.out.println("suggestResult ="
									+ suggestResult);
							if (suggestResult) {
								mHandler.sendMessage(mHandler.obtainMessage(1,
										(int) getId(), 0));
							} else {
								mHandler.sendMessage(mHandler.obtainMessage(0,
										(int) getId(), 0));
							}
						} catch (Exception e) {
							e.printStackTrace();
							mHandler.sendMessage(mHandler.obtainMessage(0, (int)id, 0));
						}
					};
				};
				loginThread.start();
			}
		});
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (id != msg.arg1) {
				return;
			}

			pd.dismiss();
			if (msg.what == 1) {
				Toast.makeText(SuggestActivity.this, R.string.publishSuccess, 3)
						.show();
				finish();
			} else {
				Toast.makeText(SuggestActivity.this, R.string.publishFail, 3)
						.show();
			}
		};
	};

	protected boolean clientValidate() {
		int suggestionLength = suggestion.getText().length();
		if (suggestionLength == 0) {
			suggestion.requestFocus();
			suggestion.setError(getString(R.string.notnull));
			return false;
		} else {
			suggestion.setError(null);
		}

		if (suggestionLength > 100) {
			suggestion.requestFocus();
			suggestion.setError(getString(R.string.suggestiontoolong));
			return false;
		} else {
			suggestion.setError(null);
		}

		return true;
	}
}
